package com.hefeng.example.service.impl;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.example.config.CommonException;
import com.hefeng.example.domain.CustomerManualWorkOrder;
import com.hefeng.example.domain.DeviceAutomaticAlarmWorkOrder;
import com.hefeng.example.domain.WorkOrderDispositionResult;
import com.hefeng.example.domain.WorkOrderDistribution;
import com.hefeng.example.mapper.CustomerManualWorkOrderMapper;
import com.hefeng.example.mapper.DeviceAutomaticAlarmWorkOrderMapper;
import com.hefeng.example.mapper.WorkOrderDispositionResultMapper;
import com.hefeng.example.mapper.WorkOrderDistributionMapper;
import com.hefeng.example.service.WorkOrderDispositionResultService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@AllArgsConstructor
public class WorkOrderDispositionResultServiceImpl extends ServiceImpl<WorkOrderDispositionResultMapper, WorkOrderDispositionResult> implements WorkOrderDispositionResultService {

    @Autowired
    private WorkOrderDispositionResultMapper workOrderDispositionResultMapper;
    @Autowired
    private DeviceAutomaticAlarmWorkOrderMapper deviceOrderMapper;
    @Autowired
    private WorkOrderDistributionMapper workOrderDistributionMapper;
    @Autowired
    private CustomerManualWorkOrderMapper customerManualWorkOrderMapper;

//    @Autowired
//    private CustomerManualWorkOrderService customerManualWorkOrderService;


    /**
     * 通过id查工单处置结果
     *
     * @param workId
     * @return
     */
    @Override
    public WorkOrderDispositionResult queryById(Integer workId) {
        LambdaQueryWrapper<WorkOrderDispositionResult> queryWrapper = new LambdaQueryWrapper<>();
        return null;
    }

    /**
     * 创建工单处置结果
     *
     * @param workOrderDispositionResult
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertR(WorkOrderDispositionResult workOrderDispositionResult) {

        boolean isTimeMake = true;
        //预期完成 5
        int status = 5;
//        LambdaQueryWrapper<WorkOrderDispositionResult>queryWrapper=new LambdaQueryWrapper<WorkOrderDispositionResult>();
//        LambdaQueryWrapper<DeviceAutomaticAlarmWorkOrder>orderQueryWrapper=new LambdaQueryWrapper<DeviceAutomaticAlarmWorkOrder>();
        LambdaQueryWrapper<WorkOrderDistribution> distQueryWrapper = new LambdaQueryWrapper<WorkOrderDistribution>();
        //判断工单完成是否超时
        //distQueryWrapper.eq(WorkOrderDistribution::getWorkId,workOrderDispositionResult.getWorkId());
        WorkOrderDistribution dis = workOrderDistributionMapper.select(workOrderDispositionResult.getWorkId());
        long betweenDay = DateUtil.between(new Date(), dis.getExpectedCompletionTime(), DateUnit.MINUTE, false);
        //isTimeMake= DateUtils.isSameInstant(dis.getExpectedCompletionTime(),new Date());
        if (betweenDay > 0) {
            //正常完成
            status = 4;
        }

        //判断工单类型 1：设备自动告警 2：四川人员转派工单
        if (workOrderDispositionResult.getWorkType().equals("1")) {
            //修改工单表状态
            String orderId = workOrderDispositionResult.getWorkId();
            DeviceAutomaticAlarmWorkOrder deviceAutomaticAlarmWorkOrder = new DeviceAutomaticAlarmWorkOrder();
            deviceAutomaticAlarmWorkOrder.setId(orderId);
            deviceAutomaticAlarmWorkOrder.setWorkStatus(status);
            deviceAutomaticAlarmWorkOrder.setUpdatedUser(workOrderDispositionResult.getUserId());
            deviceAutomaticAlarmWorkOrder.setUpdatedTime(new Date());
            deviceOrderMapper.updateAlarm(deviceAutomaticAlarmWorkOrder);
            //新增工单处理表
            workOrderDispositionResult.setStatus(String.valueOf(1));
            workOrderDispositionResult.setCreatedUser(workOrderDispositionResult.getUserId());
            workOrderDispositionResult.setCreatedTime(new Date());
            workOrderDispositionResult.setDispositionStatus(String.valueOf(1));
            workOrderDispositionResult.setDispositionTime(new Date());
            workOrderDispositionResultMapper.insert(workOrderDispositionResult);
        } else {
            //用户手动工单处理
            String orderId = workOrderDispositionResult.getWorkId();
            CustomerManualWorkOrder customerManualWorkOrder = new CustomerManualWorkOrder();
            customerManualWorkOrder.setId(orderId);
            customerManualWorkOrder.setWorkStatus(status);
            customerManualWorkOrder.setUpdatedUser(workOrderDispositionResult.getUserId());
            customerManualWorkOrder.setUpdatedTime(new Date());
            customerManualWorkOrderMapper.updateManual(customerManualWorkOrder);
            //新增工单处理表
            workOrderDispositionResult.setStatus(String.valueOf(1));
            workOrderDispositionResult.setCreatedUser(workOrderDispositionResult.getUserId());
            workOrderDispositionResult.setCreatedTime(new Date());
            workOrderDispositionResult.setDispositionStatus(String.valueOf(1));
            workOrderDispositionResult.setDispositionTime(new Date());
            workOrderDispositionResultMapper.insert(workOrderDispositionResult);

        }



//        //启用数据
//        workOrderDispositionResult.setStatus(Constant.STRING_1);
//
//        /** 更新工单状态和逾期状态 */
//        customerManualWorkOrderService.createWorkHandleResult(workOrderDispositionResult);
//
//        /** 处置时间 */
//        workOrderDispositionResult.setDispositionTime(new Date());
//
//        /** 添加处置工单信息 */
//      //  workOrderDispositionResultMapper.insertWorkOrderDispositionResult(workOrderDispositionResult);
//
//        this.save(workOrderDispositionResult);
//        return workOrderDispositionResultMapper.insertWorkOrderDispositionResult(workOrderDispositionResult);


    }


}
